/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.totalizacion;

import DBMS.DataBaseManager;
import clases.Matriz;
import clases.Totalizacion;
import clases.Unidad;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Jesus Martinez
 */
public class CargarMatrizTotalizacion extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";
    private static final String PAGE = "page";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        //Obtenemos el id de la unidad solicitante
        String unidad = (String) request.getParameter("unidad");
        DataBaseManager dbm = DataBaseManager.getInstance();
        Matriz m = (Matriz) form;
        //Si la unidad es nula, entonces se produjo un error
        //y debemos notificar a la vista.
        if (unidad == null) {
            if (m.getIdMatriz() != null) {

                ArrayList<Totalizacion> totalizaciones = dbm.consultarTotalizaciones(m.getIdMatriz());
                request.setAttribute("totalizaciones", totalizaciones);
                m = dbm.consultarMatriz(m);
                request.setAttribute("Matriz",m);
                return mapping.findForward(PAGE);
            }
            ArrayList<Unidad> unidades = dbm.getUnidades();
            request.setAttribute("unidades", unidades);
            return mapping.findForward(PAGE);
        } else {
            //Obtenemos todas las matrices asociadas a dicha unidad.
            ArrayList<Matriz> matrices = dbm.consultarMatricesPorUnidad(unidad);
            request.setAttribute("matrices", matrices);
            response.setContentType("text/html");
        }
        return mapping.findForward(SUCCESS);
        //Notificamos a la vista que se realizo exitosamente la accion.

    }
}
